package com.flyduck.modules.schedule.service.impl;

import com.flyduck.common.vo.PageResult;
import com.flyduck.common.vo.QueryVo;
import com.flyduck.modules.schedule.domain.SysScheduleJob;
import com.flyduck.modules.schedule.mapper.SysScheduleJobMapper;
import com.flyduck.modules.schedule.service.SysScheduleJobService;
import com.flyduck.modules.schedule.utils.ScheduleUtils;
import com.flyduck.modules.system.domain.SysJob;
import com.flyduck.modules.system.service.impl.BaseServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program: flyduck-admin
 * @description:
 * @author: flyduck
 * @create: 2020-06-16 16:00
 **/
@Service("sysScheduleJobService")
public class SysScheduleJobServiceImpl extends BaseServiceImpl implements SysScheduleJobService {

    @Autowired
    private SysScheduleJobMapper sysScheduleJobMapper;
    @Autowired
    private Scheduler scheduler;

    @Override
    public PageResult findPage(QueryVo queryVo) {
        //1.分页查询
        PageHelper.startPage(queryVo.getPage(),queryVo.getRows());
        Page<SysScheduleJob> sysScheduleJobPage =  sysScheduleJobMapper.findPage(queryVo.getKeyword());
        //2.封装结果集
        PageResult pageResult = new PageResult();
        pageResult.setTotal(sysScheduleJobPage.getTotal());
        pageResult.setRows(sysScheduleJobPage.getResult());
        return pageResult;
    }

    /**
     * 保存定时任务
     * @param sysScheduleJob
     */
    @Override
    public void save(SysScheduleJob sysScheduleJob) {
        sysScheduleJob.setCreateName(getUser().getUsername());
        sysScheduleJob.setCreateTime(new Date());
        sysScheduleJob.setEnabled(true);
        sysScheduleJobMapper.insert(sysScheduleJob);
        ScheduleUtils.createScheduleJob(scheduler,sysScheduleJob);
    }

    /**
     * 更新定时任务
     * @param sysScheduleJob
     */
    @Override
    public void update(SysScheduleJob sysScheduleJob) {
        ScheduleUtils.updateScheduleJob(scheduler,sysScheduleJob);
        sysScheduleJob.setUpdateName(getUser().getUsername());
        sysScheduleJob.setUpdateTime(new Date());
        sysScheduleJobMapper.updateByPrimaryKeySelective(sysScheduleJob);
    }

    /**
     * 根据定时任务id删除定时任务
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        ScheduleUtils.deleteScheduleJob(scheduler,id);
        sysScheduleJobMapper.deleteByPrimaryKey(id);
    }

    /**
     * 立即执行定时任务
     * @param id
     */
    @Override
    public void run(Long id) {
        SysScheduleJob sysScheduleJob = sysScheduleJobMapper.selectByPrimaryKey(id);
        ScheduleUtils.run(scheduler,sysScheduleJob);
    }

    /**
     * 暂停定时任务
     * @param id
     */
    @Override
    public void pause(Long id) {
        ScheduleUtils.pauseJob(scheduler,id);
        //修改状态为暂停
        sysScheduleJobMapper.updateEnabledById(false,id);
    }

    /**
     * 恢复定时任务
     * @param id
     */
    @Override
    public void resume(Long id) {
        ScheduleUtils.resumeJob(scheduler,id);
        //修改状态为运行
        sysScheduleJobMapper.updateEnabledById(true,id);
    }

    /**
     * 根据id查询定时任务
     * @param id
     * @return
     */
    @Override
    public SysScheduleJob getScheduleById(Long id) {
        return sysScheduleJobMapper.selectByPrimaryKey(id);
    }


}
